package cc.linker.steplink.crm.merchanth5.controller;

import cc.linker.steplink.crm.commons.domain.WxUser;
import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.merchanth5.commons.RequestFilter;
import cc.linker.steplink.crm.merchanth5.service.CSService;
import cc.linker.steplink.crm.response.GlobalException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description: 客服
 * @Author: wuhangfeng
 * @Date: Created in 2020/2/25 13:57
 * @Version: 1.0.20
 */
@Slf4j
@Controller
public class CSController {

    @Autowired
    private CSService csService;

    @Autowired
    private RequestFilter requestFilter;
    @Autowired
    private UserInfoUtil userInfoUtil;

    /**
     * 获取客服咨询入口
     *
     * @param belongId 商品所属ID
     * @return
     * @throws GlobalException
     */
    @RequestMapping("/h5/customer/service/entrance")
    @ResponseBody
    public Object entrance(String belongId,String goodsId,String goodsImg,Integer source,HttpServletRequest request) throws GlobalException, IOException, URISyntaxException {
        if(source==null){
            source = 1;
        }
        WxUser wxUser =  requestFilter.getWxUser(request);
        String url = csService.getEntrance(belongId,goodsId,goodsImg,wxUser,source);
        Map<String,Object> map =new HashMap<String,Object>();
        map.put("url",url);
        return map;
    }

    /**
     * 获取客服会话列表
     *
     * @param pageIndex 页码
     * @param pageSize 每页条数
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param keyWord 关键字
     * @param keyWordType 关键字类型
     * @return
     * @throws GlobalException
     */
    @GetMapping("/customer/service/getSessionList")
    @ResponseBody
    public Object getSessionList(@RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex,
                                                   @RequestParam(value = "pageSize",defaultValue = "20") Integer pageSize,
                                                   @RequestParam(value = "startTime") String startTime,
                                                   @RequestParam(value = "endTime") String endTime,
                                                   @RequestParam(value = "keyWord") String keyWord,
                                                   @RequestParam(value = "keyWordType") Integer keyWordType,
                                                   @RequestParam(value = "salerId") String salerId,
                                 HttpServletRequest request) throws GlobalException, IOException, URISyntaxException {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        return csService.getSessionList(pageIndex,pageSize,startTime,endTime,keyWordType,keyWord,salerId,userinfo.getOrgId());
    }

    /**
     * 获取客服会话详情
     *
     * @param sessionId 会话ID
     * @return
     * @throws GlobalException
     */
    @GetMapping("/customer/service/getSessionDetail")
    @ResponseBody
    public List<Map<String,Object>> getSessionDetail(@RequestParam(value = "sessionId") String sessionId,
                                                     HttpServletRequest request) throws GlobalException {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        List<Map<String,Object>> data = csService.getSessionDetail(sessionId,userinfo.getOrgId());
        return data;
    }

    /**
     * 获取客服会话详情
     *
     * @param sessionId 会话ID
     * @return
     * @throws GlobalException
     */
    @PostMapping("/customer/service/delSession")
    @ResponseBody
    public void delSession(@RequestBody  Map<String, Object> param,
                           HttpServletRequest request) throws GlobalException {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        param.put("orgId",userinfo.getOrgId());
        csService.delSession(param);
    }

    /**
     * 获取客服会话详情
     * @return
     * @throws GlobalException
     */
    @PostMapping("/customer/service/setCustomer")
    @ResponseBody
    public void setCustomer() throws Exception {
        csService.setCustomer();
    }

}
